package com.javabatch.petstore.fixtures;

import static fitnesse.util.ListUtility.list;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.javabatch.fileio.FieldError;
import com.javabatch.fileio.FileBuffer;
import com.javabatch.fileio.FileStore;
import com.javabatch.fileio.RecordBasedFileStore;
import com.javabatch.petstore.records.InvoiceRecord;

/**
 * Simple file generator for use with fitnesse.
 *
 * @author eco
 */
public class InvoiceFileFixture {
    /** Customer file. */
    private static FileStore store;


    /** order id. */
    private String id;

    private String name;

    private String total;
    private String items;
        

    public static FileStore getStore() {
		return store;
	}

	public static void setStore(FileStore store) {
		InvoiceFileFixture.store = store;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	public String getItems() {
		return items;
	}

	public void setItems(String items) {
		this.items = items;
	}

	/** Constructor to use from test when closing file. */
    public InvoiceFileFixture() {
    }

    public InvoiceFileFixture(final String file, final String readWrite) {
        if (readWrite.equalsIgnoreCase("R")) {
            store = new RecordBasedFileStore(file,
                    RecordBasedFileStore.READ_ACCESS,
                    InvoiceRecord.getRecordLength());

        } else {
            store = new RecordBasedFileStore(file,
                    RecordBasedFileStore.WRITE_ACCESS,
                    InvoiceRecord.getRecordLength());
        }
        store.open();
    }


    /**
     * Write text to file.
     *
     * @return true if ok
     */
    public String add() {
        /* Should been in the constructor, but fitnesse
         * suppresses exceptions from construcutor */
        InvoiceRecord.checkRecordLayout();

        FileBuffer outputRecord = new FileBuffer();

        InvoiceRecord.INVOICE_ID.write(outputRecord, this.id);
        InvoiceRecord.NAME.write(outputRecord, this.name);
        InvoiceRecord.TOTAL.write(outputRecord, this.total);
        InvoiceRecord.ITEMS.write(outputRecord, this.items);

        store.write(outputRecord);
        return "";
    }

    public List<Object> query() {
        List<FieldError> errors = new ArrayList<FieldError>();
        List<Object> rows = new LinkedList<Object>();
        FileBuffer inputRecord = store.readNext();

        while (inputRecord != null) {
            List<Object> aRow =
                    list(list("id", InvoiceRecord.INVOICE_ID.read(inputRecord, errors).replaceAll("^0*", "")),
                         list("name", InvoiceRecord.NAME.read(inputRecord, errors).trim()),
                         list("total", InvoiceRecord.TOTAL.read(inputRecord, errors).replaceAll("^0*", "")),
                         list("items", InvoiceRecord.ITEMS.read(inputRecord, errors).replaceAll("^0*", "")));

            if (errors.size() == 0) {
                rows.add(aRow);
            } else {
                for (FieldError error : errors) {
                    rows.add(list(list("id", error.toString())));
                }
            }

            inputRecord = store.readNext();
        }

        return rows;
    }

    public static boolean close() {
        store.close();
        return true;
    }
}
